home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / 92052tar.gz / 920528.tar / netuser.c < prev    next >
C/C++ Source or Header  |  1991-07-17  |  1KB  |  84 lines

  1. /* @(#) $Header: netuser.c,v 1.9 91/07/16 17:55:37 deyke Exp $ */
  2.  
  3. /* Miscellaneous integer and IP address format conversion subroutines
  4.  * Copyright 1991 Phil Karn, KA9Q
  5.  */
  6. #define LINELEN 256
  7. #include <ctype.h>
  8. #include <stdio.h>
  9. #include "global.h"
  10. #include "netuser.h"
  11.  
  12. int Net_error;
  13.  
  14. /* Convert Internet address in ascii dotted-decimal format (44.0.0.1) to
  15.  * binary IP address
  16.  */
  17. int32
  18. aton(s)
  19. register char *s;
  20. {
  21.     int32 n;
  22.  
  23.     register int i;
  24.  
  25.     n = 0;
  26.     if(s == NULLCHAR)
  27.         return 0;
  28.     for(i=24;i>=0;i -= 8){
  29.         /* Skip any leading stuff (e.g., spaces, '[') */
  30.         while(*s != '\0' && !isdigit(*s))
  31.             s++;
  32.         if(*s == '\0')
  33.             break;
  34.         n |= (int32)atoi(s) << i;
  35.         if((s = strchr(s,'.')) == NULLCHAR)
  36.             break;
  37.         s++;
  38.     }
  39.     return n;
  40. }
  41. /* Convert an internet address (in host byte order) to a dotted decimal ascii
  42.  * string, e.g., 255.255.255.255\0
  43.  */
  44. char *
  45. inet_ntoa(a)
  46. int32 a;
  47. {
  48.     return resolve_a(a,0);
  49. }
  50. /* Convert hex-ascii string to long integer */
  51. long
  52. htol(s)
  53. char *s;
  54. {
  55.     long ret;
  56.     char c;
  57.  
  58.     ret = 0;
  59.     while((c = *s++) != '\0'){
  60.         c &= 0x7f;
  61.         if(c == 'x')
  62.             continue;       /* Ignore 'x', e.g., '0x' prefixes */
  63.         if(c >= '0' && c <= '9')
  64.             ret = ret*16 + (c - '0');
  65.         else if(c >= 'a' && c <= 'f')
  66.             ret = ret*16 + (10 + c - 'a');
  67.         else if(c >= 'A' && c <= 'F')
  68.             ret = ret*16 + (10 + c - 'A');
  69.         else
  70.             break;
  71.     }
  72.     return ret;
  73. }
  74. char *
  75. pinet(s)
  76. struct socket *s;
  77. {
  78.     static char buf[30];
  79.  
  80.     sprintf(buf,"%s:%u",inet_ntoa(s->address),s->port);
  81.     return buf;
  82. }
  83.  
  84.